#include "compiler.hpp"
#include "runner.hpp"
#include "compiler_run.hpp"
#include "../comm/httplib.h"

using namespace httplib;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return -1;
    }

    Server svr;

    svr.Post("/compile_and_run", [](const Request &req, Response &resp){
        // 用户请求的服务正文是我们想要的json string
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty()){
            CompilerAndRun::Start(in_json, &out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });

    svr.listen("0.0.0.0", atoi(argv[1])); //启动http服务
    std::cout << "等待编译中...\n";
    // using namespace httplib;
    // Server svr;
    // svr.Get("/hello", [](const Request& req, Response& resp)
    // {
    //     resp.set_content("hello httplib", "content-type: text/plain;");
    // });
    // svr.listen("0.0.0.0", 8000);
    

    // Compiler com;
    // std::string file = "code";
    // if (!com.Compile(file))
    // {
    //     return -1;
    // }
    // // Runner run;
    // // run.Run(file);
    // Json::Value in_value;
    // std::string in_json;
    // std::string out_json;
    // in_value["code"] = R"(#include <iostream>
    // int main(){
    // int n = 5;
    // n = n / 0;
    //     std::cout << "hello" << std::endl;
    //     return 0;
    // })";
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 1024*40; //40m
    // Json::FastWriter writer;
    // in_json = writer.write(in_value);
    
    // CompilerAndRun::Start(in_json, &out_json);
    // std::cout << out_json << std::endl;
    return 0;
}